package com.qinglei.recoup.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglei.recoup.common.domain.Tree;
import com.qinglei.recoup.system.dao.DeanBedMapper;
import com.qinglei.recoup.system.domain.Bed;
import com.qinglei.recoup.system.domain.BedBO;
import com.qinglei.recoup.system.domain.BedTreeAttachBO;
import com.qinglei.recoup.system.domain.DeanBed;
import com.qinglei.recoup.system.pojo.AddDeanBedParam;
import com.qinglei.recoup.system.pojo.DeanBedVO;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.BuildingService;
import com.qinglei.recoup.system.service.DeanBedService;
import com.qinglei.recoup.system.service.FloorService;
import com.qinglei.recoup.system.service.RoomService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author syp
 */
@Service
public class DeanBedServiceImpl extends ServiceImpl<DeanBedMapper, DeanBed> implements DeanBedService {

    @Resource
    private BedService bedService;
    @Resource
    private RoomService roomService;
    @Resource
    private FloorService floorService;
    @Resource
    private BuildingService buildingService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addDeanBed(AddDeanBedParam param) {
        List<DeanBed> deanBedList = new ArrayList<>();
        for (String objIdStr : param.getObjIdList()) {
            int type = BedService.TYPE_BED;
            int objId = 0;
            if (objIdStr.equals(BedService.TREE_ALL_ID)) {
                type = BedService.TYPE_ROOT;
            } else if (objIdStr.startsWith(BedService.TREE_BUILDING_PREFIX)) {
                type = BedService.TYPE_BUILDING;
                objId = Integer.parseInt(objIdStr.substring(1));
            } else if (objIdStr.startsWith(BedService.TREE_FLOOR_PREFIX)) {
                type = BedService.TYPE_FLOOR;
                objId = Integer.parseInt(objIdStr.substring(1));
            } else if (objIdStr.startsWith(BedService.TREE_ROOM_PREFIX)) {
                type = BedService.TYPE_ROOM;
                objId = Integer.parseInt(objIdStr.substring(1));
            } else {
                objId = Integer.parseInt(objIdStr);
            }

            DeanBed deanBed = new DeanBed();
            deanBed.setUserId(param.getUserId());
            deanBed.setType(type);
            deanBed.setObjId(objId);
            deanBedList.add(deanBed);
        }
        return this.saveBatch(deanBedList);
    }

    @Override
    public Tree<Bed> getBedTree4Dean(Long userId) {
        Set<Integer> bedIds = new HashSet<>();
        Set<Integer> roomIds = new HashSet<>();
        Set<Integer> floorIds = new HashSet<>();
        Set<Integer> buildingIds = new HashSet<>();
        boolean hasTotal = false;
        List<DeanBed> deanBeds = baseMapper.selectList(Wrappers.<DeanBed>lambdaQuery().eq(DeanBed::getUserId, userId));
        for (DeanBed deanBed : deanBeds) {
            switch (deanBed.getType()) {
                case BedService.TYPE_ROOT:
                    hasTotal = true;
                    break;
                case BedService.TYPE_BUILDING:
                    buildingIds.add(deanBed.getObjId());
                    break;
                case BedService.TYPE_FLOOR:
                    floorIds.add(deanBed.getObjId());
                    break;
                case BedService.TYPE_ROOM:
                    roomIds.add(deanBed.getObjId());
                    break;
                case BedService.TYPE_BED:
                    bedIds.add(deanBed.getObjId());
                    break;
            }
        }

        BedTreeAttachBO bedTreeAttachBO = new BedTreeAttachBO();
        bedTreeAttachBO.setBedIds(bedIds);
        bedTreeAttachBO.setRoomIds(roomIds);
        bedTreeAttachBO.setFloorIds(floorIds);
        bedTreeAttachBO.setBuildingIds(buildingIds);
        bedTreeAttachBO.setHasTotal(hasTotal);
        Tree<Bed> bedTree = bedService.getBedTree(bedTreeAttachBO, true);
        return bedTree;
    }

    @Override
    public List<DeanBedVO> getDeanBedList(Long userId) {
        List<DeanBedVO> deanBedVOList = new ArrayList<>();
        List<DeanBed> deanBeds = baseMapper.selectList(Wrappers.<DeanBed>lambdaQuery().eq(DeanBed::getUserId, userId)
                .orderByDesc(DeanBed::getType));
        for (DeanBed deanBed : deanBeds) {
            DeanBedVO deanBedVO = new DeanBedVO();
            deanBedVO.setId(deanBed.getId());
            String name = "";
          try {
              switch (deanBed.getType()) {
                  case BedService.TYPE_ROOT:
                      name = "全部床位";
                      break;
                  case BedService.TYPE_BUILDING:
                      name = buildingService.getNameWithParent(deanBed.getObjId()) + "-全部床位";
                      break;
                  case BedService.TYPE_FLOOR:
                      name = floorService.getNameWithParent(deanBed.getObjId()) + "-全部床位";
                      break;
                  case BedService.TYPE_ROOM:
                      name = roomService.getNameWithParent(deanBed.getObjId()) + "-全部床位";
                      break;
                  case BedService.TYPE_BED:
                      BedBO bedBO = bedService.getBedFullName(deanBed.getObjId());
                      System.out.println(userId);
                      System.out.println(deanBed.getObjId());
                      name = bedBO.getBuildingName() + "-" + bedBO.getFloorName() + "-" + bedBO.getRoomName() + "-" + bedBO.getBedName();
                      break;
              }
              deanBedVO.setName(name);
              deanBedVOList.add(deanBedVO);
          }catch (Exception e){
              e.printStackTrace();
          }
        }
        return deanBedVOList;
    }

}
